import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

const initialState = {
  value: 1,
};

export const asyncAddCount = createAsyncThunk(
  "counter/asyncAddCount",
  async () => {
    await new Promise((resolve) => setTimeout(resolve, 2000));
  }
);

const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    addCount(state) {
      state.value++;
    },
  },
  extraReducers: {
    [asyncAddCount.fulfilled.type](state) {
      state.value += 10;
    },

    [asyncAddCount.pending.type](state) {
      state.value += 10;
    },

    [asyncAddCount.rejected.type](state) {
      state.value += 10;
    },
  },
});

export const { addCount } = counterSlice.actions;

export default counterSlice;
